package bsts.controller;

import java.util.ArrayList;
import bsts.model.Complaint;
/*
 * Author 		: Lee Zhong De Rollei
 * Student ID	: A0079953L
 */
public class ComplaintController {
	/*Local attributes===================================================================================================*/
	private static ArrayList<Complaint> complaints;
	private static IOController ioController;
	
	/*Constructors=======================================================================================================*/
	public ComplaintController() {
		ioController = new IOController();
		ComplaintController.complaints = ioController.RetrieveComplaints() ;
	}

	/*Private methods use to get current ID=============================================================================*/
	private static Integer getCurrent_ComplaintID(){		
		Integer temp_ComplaintID;
		ArrayList<Complaint> tempComplaints = new ArrayList<Complaint>();
		ioController = new IOController();
		
		tempComplaints = ioController.RetrieveComplaints();
		
		if (tempComplaints == null) {
			temp_ComplaintID = new Integer(0);
		}else{
			temp_ComplaintID = new Integer(10000+tempComplaints.size());
		}
		
		return temp_ComplaintID;
	}
	
	
	/*ALL IO Methods to handle complaints===============================================================================*/
	public boolean UpdateComplaint(Complaint complaint){
		for(int i=0; i<complaints.size();i++){
			Complaint comp = complaints.get(i);
			if (comp.getComplaintId()==complaint.getComplaintId())
			{							
				//Remove from Arraylist
				complaints.remove(i);
				//Insert the update object to Arraylist
				complaints.add(complaint);
				//Update file
				return ioController.UpdateComplaints(ComplaintController.complaints);				
			}			
		}//end of loop
		return false;				
	}//end of function
	
	public boolean AddComplaint(Complaint complaint){
		complaints.add(complaint);
		return ioController.UpdateComplaints(ComplaintController.complaints);					
	}
		
	public Complaint GetComplaint(long complaintId){
		for(int i=0; i<complaints.size();i++){
			Complaint comp = complaints.get(i);
			if (comp.getComplaintId()==complaintId)
			{							
				return comp;		
			}			
		}//end of loop
		return null;
	}//end of function

	public ArrayList<Complaint> getComplaints() {
		return complaints;
	}

	public void setComplaints(ArrayList<Complaint> complaints) {
		ComplaintController.complaints = complaints;
	}
	
	public static Integer getNext_ComplaintID(){
		return getCurrent_ComplaintID()+1;
	}

}
